{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import visa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "rm=visa.ResourceManager()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TCS:\n",
    "    def __init__(self, address):\n",
    "        self.address = address\n",
    "        self.visa = rm.open_resource(\"ASRL%d::INSTR\" % address)\n",
    "        \n",
    "        self.heater = [0,0,0]\n",
    "        self.current = [0,0,0]\n",
    "        self.current_range = [1,1,1]\n",
    "    \n",
    "    def status(self):\n",
    "        answer = self.visa.query(\"STATUS?\")\n",
    "        answer = answer.split(\"\\t\")[1]\n",
    "        answer = answer.split(\",\")\n",
    "        answer = list(map(int, answer)) # convert all strings into integers\n",
    "        \n",
    "        self.current_range = answer[1::4]\n",
    "        self.current = answer[2::4]\n",
    "        self.heater = answer[3::4]\n",
    "        \n",
    "        range_factor = [1,10,100,1000]\n",
    "        \n",
    "        for i in range(3):\n",
    "            self.current[i] = self.current[i]*range_factor[self.current_range[i]-1]\n",
    "        \n",
    "    \n",
    "    def sorb(self, current=None):\n",
    "        if current != None:\n",
    "            self.visa.write(\"SETDAC 1 0 %d\\t\" % current)\n",
    "            \n",
    "        self.status()\n",
    "        return self.heater[0], self.current[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "tcs=TCS(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 10000)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tcs.sorb()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "answer = tcs.visa.query(\"STATUS?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '3', '50', '1', '0', '4', '0', '0', '0', '4', '0', '0', '0\\n']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answer = answer.split(\"\\t\")[1]\n",
    "answer = answer.split(\",\")\n",
    "answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 50, 1, 0, 4, 0, 0, 0, 4, 0, 0, 0]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answer = list(map(int, answer)) # convert all strings into integers\n",
    "answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "        current_range = answer[1::4]\n",
    "        current = answer[2::4]\n",
    "        heaters = answer[3::4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "        range_factor = [1,10,100,1000]\n",
    "        \n",
    "        for i in range(3):\n",
    "            heater[i] = heaters[i]\n",
    "            current[i] = current[i]*range_factor[current_range[i]-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5000, 0, 0]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "current"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18, <StatusCode.success: 0>)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tcs.visa.write(\"SETDAC 1 0 10000\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SETDAC 1 0 5000\n"
     ]
    }
   ],
   "source": [
    "current = 5000\n",
    "print(\"SETDAC 1 0 %d\" % current)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\tdef SetTCS(self,Source,Current):\n",
    "\t\tif Current < 0:\n",
    "\t\t\tCurrent = 0\n",
    "\t\telif Current > self.MaxCurrent:\n",
    "\t\t\tCurrent = self.MaxCurrent\n",
    "\t\t# Current in microAmp\n",
    "\t\t# print Current\n",
    "\t\tSource = Source + 1\n",
    "\t\tcommand = \" \".join((\"SETDAC\",\"%d\" % Source,\"0\",\"%d\" % Current))\n",
    "\t\t\n",
    "\t\tself.TCSVisa.ask(command)\n",
    "\t\treturn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class PicoWatt:\n",
    "    def __init__(self address):\n",
    "        self.address = address\n",
    "        self.visa = rm.open_resource(\"GPIB::%d::INSTR\" % address)\n",
    "    \n",
    "    def read_temperature(self, )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\tdef ReadTCS(self):\n",
    "\t\tAnswer = self.TCSVisa.ask(\"STATUS?\")\n",
    "\t\tReply = Answer.split(\"\\t\")[1]\n",
    "\t\tReply = Reply.split(\",\")\n",
    "\t\tRange = Reply[1::4]\n",
    "\t\tCurrent = Reply[2::4]\n",
    "\t\tHeaters = Reply[3::4]\n",
    "\t\tTMP = [1,10,100,1000]\n",
    "\t\tfor i in range(3):\n",
    "\t\t\tself.TCSHeater[i] = int(Heaters[i])\n",
    "\t\tfor i in range(3):\n",
    "\t\t\tself.TCSCurrent[i] = int(Current[i])*TMP[int(Range[i])-1]\n",
    "\t\treturn"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
